namespace RushMail.Requests;

public class Template
{
    private readonly IRushMailClient _client;

    public Template(IRushMailClient client)
    {
        _client = client;
    }

    /// <summary>
    /// 模板列表
    /// http://edmapi.rushmail.com/api.php?module=wc-template&action=template-list
    /// </summary>
    /// <returns></returns>
    public async Task<RushMailResponse<TemplateListResponse>> List() =>
        (await _client.PostAsync(new RushMailRequest("wc-template", "template-list"))).ToGeneric<TemplateListResponse>();
    
    /// <summary>
    /// 模板详情
    /// http://edmapi.rushmail.com/api.php?module=wc-template&action=template-view
    /// </summary>
    /// <returns></returns>
    public async Task<RushMailResponse<object>> View(int templateId) =>
        (await _client.PostAsync(new RushMailRequest("wc-template", "template-view")
        {
            Body = new Dictionary<string, string>() { {"template_id", templateId.ToString()} }
        }))
        .ToGeneric<object>();    
    
    /// <summary>
    /// 模板新增
    /// http://edmapi.rushmail.com/api.php?module=wc-template&action=template-add
    /// </summary>
    /// <returns></returns>
    public virtual async Task<RushMailResponse<int>> Add(TemplateAdd body) =>
        (await _client.PostAsync(new RushMailRequest("wc-template", "template-add")
        {
            Body = body.ToDictionary()
        }))
        .ToGeneric<int>();
    
    /// <summary>
    /// 模板修改
    /// http://edmapi.rushmail.com/api.php?module=wc-template&action=template-edit
    /// </summary>
    /// <returns></returns>
    public Task<RushMailResponse> Edit(TemplateEdit body) =>
        _client.PostAsync(new RushMailRequest("wc-template", "template-edit")
        {
            Body = body.ToDictionary()
        });
    
    /// <summary>
    /// 模板删除
    /// http://edmapi.rushmail.com/api.php?module=wc-template&action=template-delete
    /// </summary>
    /// <returns></returns>
    public Task<RushMailResponse> Delete(int templateId) =>
        _client.PostAsync(new RushMailRequest("wc-template", "template-delete")
        {
            Body = new Dictionary<string, string>() { {"template_id", templateId.ToString()} }
        });
    
    /// <summary>
    /// 上传模板附件(在线附件
    /// http://edmapi.rushmail.com/api.php?module=wc-template&action=attachment-upload
    /// </summary>
    /// <returns></returns>
    public Task<RushMailResponse> AttachmentUpload(int templateId) =>
        _client.PostAsync(new RushMailRequest("wc-template", "template-delete")
        {
            Body = new Dictionary<string, string>() { {"template_id", templateId.ToString()} }
        });
    
    /// <summary>
    /// 上传模板附件(系统附件)
    /// http://edmapi.rushmail.com/api.php?module=wc-template&action=attachment-upload-system
    /// </summary>
    /// <returns></returns>
    public Task<RushMailResponse> AttachmentUploadSystem(int templateId) =>
        _client.PostAsync(new RushMailRequest("wc-template", "template-delete")
        {
            Body = new Dictionary<string, string>() { {"template_id", templateId.ToString()} }
        });
    
    /// <summary>
    /// 删除模板附件
    /// http://edmapi.rushmail.com/api.php?module=wc-template&action=attachment-delete
    /// </summary>
    /// <returns></returns>
    public Task<RushMailResponse> AttachmentDelete(int templateId) =>
        _client.PostAsync(new RushMailRequest("wc-template", "template-delete")
        {
            Body = new Dictionary<string, string>() { {"template_id", templateId.ToString()} }
        });
}


public class TemplateAdd
{
    public int CategoryId { get; set; }
    public string Name { get; set; }
    /// <summary>
    /// editor 或 files
    /// </summary>
    public string Mode { get; set; }
    public string Subject { get; set; }
    public string[] Subjects { get; set; }
    public string TplContent { get; set; }
    public string TplFile { get; set; }

    public virtual Dictionary<string, string> ToDictionary() => new Dictionary<string, string>()
    {
        { "category_id", CategoryId.ToString()},
        { "name", Name},
        { "mode", Mode},
        { "subject", Subject },
        { "tpl_content", TplContent },
        { "tpl_file", TplFile },
    };
}

public class TemplateEdit : TemplateAdd
{
    public int TemplateId { get; set; }

    public override Dictionary<string, string> ToDictionary()
    {
        var dic = base.ToDictionary();
        dic.Add("template_id", TemplateId.ToString());
        return dic;
    }
}

public class TemplateListResponse
{
    [JsonProperty("template_list")]public TemplateListPage TemplateList { get; set; }
}

public class TemplateListPage
{
    public List<TemplateItem> List { get; set; }
    public Paginate Page { get; set; }
}

public class TemplateItem
{
    [JsonProperty("template_id")]
    public int TemplateId { get; set; }    
    [JsonProperty("account_id")]
    public int AccountId { get; set; }    
    [JsonProperty("category_id")]
    public int CategoryId { get; set; }
    public int Examine { get; set; }
    
    public string Name { get; set; }
    public string Size { get; set; }
    public int Score { get; set; }
    public int Cost { get; set; }
    public DateTime Updated { get; set; }
}
